package com.an.framework;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * @author Administrator
 * 将DispatcherServlet配置在Servlet容器中，而不在使用web。xml
 */
public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
		//, DataSourceConfig.class, MyBatisConfig.class
		return new Class[] { SpringSecurityConfig.class };
	}

	/* 指定配置类
	 * @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer#getServletConfigClasses()
	 */
	@Override
	protected Class<?>[] getServletConfigClasses() {
		return null;
	}

	/*
	 * 将一个或者多个路径映射到DispactherServlet上，以下配置映射的是“/”，
	 * 这表示它会是应用的默认Servlet，他会处理进入应用的所有请求。
	 * 
	 * @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer#getServletMappings()
	 */
	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}

	@Override
	protected void customizeRegistration(Dynamic registration) {
		// 设置参数 配置404
		boolean done = registration.setInitParameter("throwExceptionIfNoHandlerFound", "true"); // -> true
		if (!done) {
			throw new RuntimeException();
		}
		
		//设置文件上传下载的位置
		registration.setMultipartConfig(new MultipartConfigElement("D:\\uploads"));
	}
	
	

}
